<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use \dmstr\bootstrap\Tabs;
use yii\helpers\StringHelper;
use \trntv\filekit\widget\Upload;
use yii\bootstrap\Modal;
use yii\redactor;
use yii\helpers\Url;
/**
* @var yii\web\View $this
* @var yiike\goods\model\Goods $model
* @var yii\widgets\ActiveForm $form
*/

?>

<div class="goods-form">

    <?php $form = ActiveForm::begin([
    'id' => 'Goods',
    'layout' => 'horizontal',
    'enableClientValidation' => true,
    'errorSummaryCssClass' => 'error-summary alert alert-danger',
    'fieldConfig' => [
             'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
             'horizontalCssClasses' => [
                 'label' => 'col-sm-2',
                 #'offset' => 'col-sm-offset-4',
                 'wrapper' => 'col-sm-8',
                 'error' => '',
                 'hint' => '',
             ],
         ],
    ]
    );
    ?>

    <div class="">
        <?php $this->beginBlock('basic'); ?>

        <p>
           <?= $form->field($model,'content')->widget('kucha\ueditor\UEditor',[]); ?>
<!-- attribute title -->
			<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
			<!-- attribute sub_title -->
			<?= $form->field($model, 'sub_title')->textInput(['maxlength' => true]) ?>

			<!-- attribute short_title -->
			<?= $form->field($model, 'short_title')->textInput(['maxlength' => true]) ?>

			<!-- attribute keyword -->
			<?= $form->field($model, 'keyword')->textInput(['maxlength' => true]) ?>
            <?= $form->field($model, 'orderby')->textInput(['maxlength' => true]) ?>
			<!-- attribute type -->
            <?= $form->field($model, 'type')->radioList(['0'=>'线下','1'=>'线上'],['class'=>'label-group'])->label('商品类型'); ?>


        </p>
        <?php $this->endBlock(); ?>
		<?php $this->beginBlock('goods'); ?>
            <p>
			<!-- attribute cid -->
                <?= $form->field($model, 'cid')->dropDownList($category)?>

			<?= $form->field($model, 'tags')->label('标签')->checkboxList($tags) ?>

			<?=
			$form->field($model, 'imgarr')->label('图片')->widget(
					Upload::className(), [
							'url'              => ['/files/upload'],
							'sortable'         => true,
							'maxFileSize'      => 10000000, // 10 MiB
							'maxNumberOfFiles' => 4,

					]
			);
		?>

			<!-- attribute price -->
			<?= $form->field($model, 'price')->textInput(['maxlength' => true]) ?>

			<!-- attribute orig_price -->
			<?= $form->field($model, 'orig_price')->textInput(['maxlength' => true]) ?>

			<!-- attribute cost_price -->
			<?= $form->field($model, 'cost_price')->textInput(['maxlength' => true]) ?>

            <!-- attribute sold -->
            <?= $form->field($model, 'sold')->textInput() ?>


            </p>
			<?php $this->endBlock() ?>

            <?php $this->beginBlock('property'); ?>
            <p>
            <!-- attribute full_num_shipping -->
            <?= $form->field($model, 'full_num_shipping',[
                'template' => "{label}\n<div class=\"col-sm-8 \"><div class=\"input-group fixsingle-input-group\">
                    <span class=\"input-group-addon\">满</span>{input}<span class=\"input-group-addon\">件</span>
                    </div><p class=\"help-block \">如果设置0或空，则不支持满件包邮</p>{error}</div>",
                'labelOptions' => ['class' => 'col-lg-2 control-label'],
            ])->label('单品满件包邮') ?>

            <!-- attribute full_money_shipping -->
            <?= $form->field($model, 'full_money_shipping',[
                'template' => "{label}\n<div class=\"col-sm-8 \"><div class=\"input-group fixsingle-input-group\">
                    <span class=\"input-group-addon\">满</span>{input}<span class=\"input-group-addon\">元</span>
                    </div><p class=\"help-block \">如果设置0或空，则不支持满额包邮</p>{error}</div>",
                'labelOptions' => ['class' => 'col-lg-2 control-label'],
            ])->textInput(['maxlength' => true])->label('单品满额包邮') ?>
            <!-- attribute no_shipping -->
            <?= $form->field($model, 'no_shipping')->textarea(['rows' => 1]) ?>
                <?php


                Modal::begin([
                    'id' => 'page-modal',
                    'header' => '<h5>选择区域</h5>',
                    'footer' => '<a href="javascript:;" id="btnSubmitArea" class="btn btn-success" data-dismiss="modal" aria-hidden="true">确定</a>',
                    'toggleButton' => ['label' => '添加不包邮地区','style'=>'margin-left:400px;margin-bottom:10px;','id'=>'setArea'],
                    'bodyOptions'=>['style'=>'height:420px;'],
                ]);

                echo $provincesHtml;

                Modal::end();
                ?>
            <!-- attribute shipping_fee -->
            <?= $form->field($model, 'shipping_fee')->textInput(['maxlength' => true]) ?>
                <?=
                $form->field($model, '地区')->widget(\chenkby\region\Region::className(),[
                    'model'=>$model,
                    'url'=> \yii\helpers\Url::toRoute(['get-region']),
                    'province'=>[
                        'attribute'=>'province',
                        'items'=> \common\models\Region::getRegion(),
                        'options'=>['class'=>'form-control form-control-inline','prompt'=>'选择省份']
                    ],
                    'city'=>[
                        'attribute'=>'city',
                        'items'=>\common\models\Region::getRegion($model['province']),
                        'options'=>['class'=>'form-control form-control-inline','prompt'=>'选择城市']
                    ],
                    'district'=>[
                        'attribute'=>'district',
                        'items'=>\common\models\Region::getRegion($model['city']),
                        'options'=>['class'=>'form-control form-control-inline','prompt'=>'选择县/区']
                    ]
                ]);
                ?>
            <!-- attribute invoice -->
            <?= $form->field($model, 'invoice')->radioList(['0'=>'否','1'=>'是'],['class'=>'label-group'])->label('是否有发票'); ?>


                <!-- attribute status -->
            <?= $form->field($model, 'status')->radioList(['0'=>'否','1'=>'是'],['class'=>'label-group'])->label('是否上架'); ?>

                <!-- attribute hide_search -->
            <?= $form->field($model, 'hide_search')->radioList(['0'=>'否','1'=>'是'],['class'=>'label-group'])->label('搜索列表不显示'); ?>


            <!-- attribute no_return -->
            <?= $form->field($model, 'no_return')->radioList(['0'=>'否','1'=>'是'],['class'=>'label-group'])->label('不能退货'); ?>


            </p>
            <?php $this->endBlock(); ?>
        <?php $this->beginBlock('sku'); ?>
        <p>

            <!-- attribute goods_sn -->
            <?= $form->field($model, 'goods_sn',[
                'template' => "{label}\n<div class=\"col-sm-8 \">
                   {input}
                    <p class=\"help-block \"></p>{error}</div>",
                'labelOptions' => ['class' => 'col-lg-2 control-label'],'inputOptions' => ['class' => 'form-control stock']
            ])->textInput(['maxlength' => true])->label('商品编码') ?>

            <!-- attribute weight -->
            <?= $form->field($model, 'weight',[
                'template' => "{label}\n<div class=\"col-sm-8 \">
                   {input}
                    <p class=\"help-block \"></p>{error}</div>",
                'labelOptions' => ['class' => 'col-lg-2 control-label'],'inputOptions' => ['class' => 'form-control stock']
            ])->textInput(['maxlength' => true])->label('重量') ?>


            <!-- attribute stock -->
            <?= $form->field($model, 'stock',[
                'template' => "{label}\n<div class=\"col-sm-8 \">
                   {input}
                    <p class=\"help-block \"></p>{error}</div>",
                'labelOptions' => ['class' => 'col-lg-2 control-label'],'inputOptions' => ['class' => 'form-control stock']
            ])->textInput(['maxlength' => true])->label('库存') ?>
        <div class="form-group" >
            <label class="control-label col-sm-2" for="goods-stock"></label>
            <div class="col-sm-8">
            <p class="help-block">启用多规格后，上述设置无效.</p>
                </div>
            </div>
            <!-- attribute stock_rule -->
            <?= $form->field($model, 'stock_rule')->radioList(['1'=>'拍下减库存','2'=>'付款减库存','3'=>'永不减库存'],['class'=>'label-group'])->label('减库存规则'); ?>

            <!-- attribute is_spec -->
        <div class="form-group">
            <label class="col-xs-12 col-sm-3 col-md-2 control-label">是否启用多规格</label>
            <div class="col-sm-4">
                <label  class="checkbox-inline">
                    <input name='is_spec' type="checkbox" value="1" <?php if($is_spec==1){ echo 'checked';}?>/>启用

                </label>
                <label class="checkbox-inline"><a  class="btn-info btn add_value" style="display:none">添加多规格</a></label>
            </div>
        </div>
        <div id="spec_val" style="display:none;">
            <div class="form-group">
                <label class="col-xs-12 col-sm-3 col-md-2 control-label">规格值</label>
                <div class="col-md-8">
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th width="10%">规格名 *</th>
                            <th>售价 *</th>
                            <th>原价</th>
                            <th>成本价</th>
                            <th>库存</th>
                            <th>编码</th>
                            <th>重量(千克)</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="spec_add">
                        <?php if($SpecData){?>
                            <?php foreach($SpecData as $index=>$row){ ?>
                        <tr>
                            <td>
                                <input type="hidden" class="form-control"  name="spec_id[]"  value="<?php echo $row['id']; ?>"  />
                                <input type="text" class="form-control"  name="spec_name[]"  value="<?php echo $row['name']; ?>" required  />
                            </td>
                            <td>
                                <input type="number" min="0.0" step="0.01"  class="form-control" name="spec_price[]"  value="<?php echo $row['price'];?>" required />
                            </td>
                            <td>
                                <input class="form-control" type="number" min="0.0" step="0.01"  name="spec_orig_price[]"  value="<?php echo $row['orig_price'];?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="number" min="0.0" step="0.01"  name="spec_cost_price[]"  value="<?php echo $row['cost_price']?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="number"  name="spec_stock[]"  value="<?php echo $row['stock']?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="text"  name="spec_goods_sn[]"  value="<?php echo $row['goods_sn'];?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="number" min="0.0" step="0.01"  name="spec_weight[]"  value="<?php echo $row['weight']?>"  />
                            </td>
                            <td>
                                <a href="javascript:;" class='btn-info btn del_spec ajax_del' url="<?php echo Url::to(['goods/spec-del', 'id' => $row['id']]); ?>" title="删除" aria-label="删除" data-pjax="0"  data-method="post">删除</a>
                               <!-- <a  class='btn-info btn del_spec ajax_del' data-id="<?php /*echo $row['id']*/?>" >删除</a>-->
                            </td>
                        </tr>
                        <?php } ?>
                        <?php } ?>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        </p>
        <?php $this->endBlock(); ?>
        <?php $this->beginBlock('info'); ?>
        <p>
            <!-- attribute content -->

        </p>
        <?php $this->endBlock(); ?>
        <?php $this->beginBlock('param'); ?>
        <p>

<div class="form-group">
    <label class="col-xs-12 col-sm-4 col-md-3 control-label"></label>
    <div class='col-sm-3'>
        <a class='add_param btn-info btn' href='javascript:;' style="align:center;">添加参数项</a>
    </div>
</div>

<?php if($property){?>
<?php  foreach($property as $index =>$row){?>
<div class="form-group">
    <label class="col-xs-12 col-sm-3 col-md-2 control-label">参数</label>
    <div class="col-sm-9">
        <div class='col-sm-3'>
            <input type='text' name='param_name[]' placeholder="参数名称" value="<?php echo $row['name']; ?>"  class='form-control'  />
        </div>
        <div class='col-sm-3'>
            <input type='text' name='param_val[]' placeholder="参数值" value="<?php echo $row['val']; ?>" class='form-control' />
        </div>
        <a class='del_param btn-default btn' href='javascript:;'>删除</a>
    </div>
</div>
<?php } ?>
<?php } ?>

        </p>
        <?php $this->endBlock(); ?>
        <?php $this->beginBlock('power'); ?>
        <p>

            <!-- attribute max_buy -->
            <?= $form->field($model, 'max_buy')->textInput()->label('单次最多购买') ?>

            <!-- attribute min_buy -->
            <?= $form->field($model, 'min_buy')->textInput()->label('单次最低购买') ?>

        </p>
        <?php $this->endBlock(); ?>
        <?php $this->beginBlock('markting'); ?>
        <p>
            <!-- attribute quan_rule -->
            <?= $form->field($model, 'quan_rule')->textInput()->label('可用券抵消额度') ?>
            <!-- attribute give_credit1 -->
            <?= $form->field($model, 'give_credit1')->textInput()->label('佣金让利') ?>
            <!-- attribute commision -->
            <?= $form->field($model, 'commision')->textInput(['maxlength' => true])->label('赠送彩豆数') ?>

        </p>
        <?php $this->endBlock(); ?>

		<?php $this->beginBlock('share'); ?>
        <p>
        <!-- attribute share_title -->
        <?= $form->field($model, 'share_title')->textInput(['maxlength' => true]) ?>
		<!-- attribute share_image -->
            <?=
            $form->field($model, 'shareImg')->label('分享图片')->widget(
                Upload::className(), [
                    'url'              => ['/files/upload'],
                    'sortable'         => true,
                    'maxFileSize'      => 10000000, // 10 MiB
                    'maxNumberOfFiles' => 1,

                ]
            );
            ?>
		<!-- attribute share_content -->
		<?= $form->field($model, 'share_content')->textArea(['maxlength' => true]) ?>
        </p>
		<?php $this->endBlock(); ?>
        <?=
    Tabs::widget(
                 [
                    'encodeLabels' => false,
                    'items' => [ 
                        [
							'label'   => Yii::t('models', '基本信息'),
							'content' => $this->blocks['basic'],
							 'active'  => true,
				],
						[
							'label'   => Yii::t('models', '商品信息'),
							'content' => $this->blocks['goods'],
							'active'  =>false,
						],
                        [
                            'label'   => Yii::t('models', '商品属性'),
                            'content' => $this->blocks['property'],
                            'active'  =>false,
                        ],
                        [
                            'label'   => Yii::t('models', '库存规格'),
                            'content' => $this->blocks['sku'],
                            'active'  =>false,
                        ],
                        [
                            'label'   => Yii::t('models', '商品详情'),
                            'content' => $this->blocks['info'],
                            'active'  =>false,
                        ],
                        [
                            'label'   => Yii::t('models', '商品参数'),
                            'content' => $this->blocks['param'],
                            'active'  =>false,
                        ],
                        [
                            'label'   => Yii::t('models', '购买权限'),
                            'content' => $this->blocks['power'],
                            'active'  =>false,
                        ],
                        [
                            'label'   => Yii::t('models', '营销'),
                            'content' => $this->blocks['markting'],
                            'active'  =>false,
                        ],
						[
							'label'   => Yii::t('models', '分享'),
							'content' => $this->blocks['share'],
							'active'  =>false,
						],

                    ]
                 ]
    );
    ?>
        <hr/>

        <?php echo $form->errorSummary($model); ?>

        <?= Html::submitButton(
        '<span class="glyphicon glyphicon-check"></span> ' .
        ($model->isNewRecord ? '添加' : '保存'),
        [
        'id' => 'save-' . $model->formName(),
        'class' => 'btn btn-success'
        ]
        );
        ?>

        <?php ActiveForm::end(); ?>

        <?php $this->beginBlock('goodsEdit') ?>
            var isChecked = $("input[name='is_spec']").is(":checked");
            if(isChecked){
            $('.stock').attr('disabled',true);
            $('#spec_val').css('display','block');
            $('.add_value').css('display','block');
            }else{
            $('.stock').attr('disabled',false);
            $('#spec_val').css('display','none');
            $('.add_value').css('display','none');
            }
            $('#setArea').unbind('click').click(function(){
            $('#goods-no_shipping').val('');
                citystrs = '';
            })
            $('#btnSubmitArea').unbind('click').click(function(){
             $('#goods-no_shipping').val('');
            citystrs = ''
            $('.cityall:checked').each(function(){

                citystrs+= $(this).val() +",";
                });
                $('#goods-no_shipping').val(citystrs);
            })
            //多规格
            $("input[name='is_spec']").click(function(){
            var isChecked = $(this).is(":checked");
            if(isChecked){
            $('.stock').attr('disabled',true);
            $('#spec_val').css('display','block');
            $('.add_value').css('display','block');
            }else{
            $('.stock').attr('disabled',false);
            $('#spec_val').css('display','none');
            $('.add_value').css('display','none');
            }
            })

            //添加规格
            $('.add_value').click(function(){
            var html = "<tr>";
                html+="<td><input type='text' class='form-control'  name='spec_name[]' required /> </td>" ;
                html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_price[]' required /></td>";
                html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_orig_price[]'  /></td>";
                html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_cost_price[]'  /></td>";
                html+="<td><input type='number' class='form-control' name='spec_stock[]'  /></td>";
                html+="<td><input type='text' class='form-control' name='spec_goods_sn[]'  /></td>";
                html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_weight[]'  /></td>";
                html+="<td><a  class='btn-info btn del_spec'>删除</a></td>";
                html+="</tr>";
            $('#spec_add').append(html);
            })

            //删除规格
            $('tbody').on('click','.del_spec',function(){
            $(this).parents('tr').remove();
            });
            //添加参数
            $('.add_param').click(function(){
            var html = "<div class='form-group'>";
                html+="<label class='col-xs-12 col-sm-3 col-md-2 control-label'></label>";
                html+="<div class='col-sm-9'>";
                    html+="<div class='col-sm-3'><input type='text' name='param_name[]' placeholder='参数名称'  class='form-control'  /> </div>" ;
                    html+="<div class='col-sm-3'><input type='text' name='param_val[]' placeholder='参数值' class='form-control'/></div>";
                    html+="<a class='del_param btn-default btn' href='javascript:;'>删除</a>";
                    html+="</div></div>";
            $('#w2-tab5').append(html);
            })
            //删除参数
            $('#w2-tab5').on('click','.del_param',function(){
            $(this).parents('.form-group').remove();
            });

            $(function () {
            $('.ajax_del').click(function () {
            var tThis =$(this);
            if (confirm("确定要删除此项规格？")){
            var url = tThis.attr('url');
            $.get(url,function (data) {
            if (data == true){
            $(Tthis).parents('tr').remove();
            alert('删除成功');
            }else{
            alert('删除失败');
            }
            })
            }else{
            return false;
            }

            })
            })
        <?php $this->endBlock() ?>
        <?php $this->registerJs($this->blocks['goodsEdit']); ?>

<script>


</script>
    </div>

</div>

